﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using fideeraSoft.Utils;
using fideeraSoft.Entities;
using fideeraSoft.Handlers;
using System.Data;
using System.Data.SqlClient;

namespace fideeraSoft.Paneles
{
    class ProveedorPanelModificar:PanelGeneral
    {
        private DataGridView dgvMostrarProveedores;
        private TableLayoutPanel tlpBuscarProv;
        private TextBox tbBuscarProvCuil;
        
        private TableLayoutPanel tlpBuscarProvRazon;
            
        private TextBox tbBuscarProvRazonS;
        private Button btBuscarProvCuil;
        private Button btBuscarProvRaz;
       
        private DataSet ds;
        private DataSet changes;
        private Button btGuardarCambios;
        private SqlDataAdapter adapter;
        //private BindingSource bsData;
        private int locationTlpBuscarProv;
        private int locationBtCambios = 0;
        private int locationTlpBuscarProvRazon = 0;
        private TableLayoutPanel tlpBotones;
        private Button btVolver;
        private SqlCommandBuilder cmdBuilder;
        
        public ProveedorPanelModificar(Handler handler)
        {
            this.formatearPanelGeneral(handler); 

            try
            {
                InitializeComponent();
                this.formatearTableTitulo("MODIFICACIÓN DE PROVEEDOR");
               
                string query = "SELECT ID AS [ID Proveedor],razonSocial AS [Razón Social], responsable AS Responsable, cuil AS Cuil, direccion AS Dirección,localidad AS Localidad, provincia AS Provincia, telefono AS Teléfono, celular AS Celular FROM Proveedor";
               adapter= SQLclass.obtenerAdapter(query);
               if (adapter != null)
               {
                   ds = new DataSet();
                   adapter.Fill(ds);
                   
                   

                   if (ds.Tables[0].Rows.Count <= 0)
                       MessageBox.Show("NO HAY NINGUN PROVEEDOR CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


                   dgvMostrarProveedores.DataSource = ds.Tables[0];

                   formatearDataGrid();

                   formatearTablaBuscarProv();
                                      
                   formatearTablaBuscarRazon();

                   formatearTablaBotones();

                   
                   this.Controls.Add(dgvMostrarProveedores);
                   this.Controls.Add(tlpBuscarProv);
                   this.Controls.Add(tlpBuscarProvRazon);
                   this.Controls.Add(tlpBotones);

               }

                else {
                    //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                    MessageBox.Show("NO HAY NINGUN PROVEEDOR CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.handler.backToPrincipal();
                }
                
            }
            catch (MiException ex)
            {

                CLogger.Append("[ProveedorPanelModificar] Exception: " + ex, CLogger.ERROR);      //DEBUG
            
            }            
        }

        #region FORMATEAR
                
        private void formatearDataGrid()
        {
            
            dgvMostrarProveedores.AutoGenerateColumns = true;
            dgvMostrarProveedores.Size = new Size(this.Size.Width - 100, this.Size.Height - 400);
            dgvMostrarProveedores.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrarProveedores.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrarProveedores.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrarProveedores.BorderStyle = BorderStyle.Fixed3D;
           
            dgvMostrarProveedores.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrarProveedores.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaBuscarProv()
        {
            tlpBuscarProv.Controls.Add(btBuscarProvCuil);
            tlpBuscarProv.Controls.Add(tbBuscarProvCuil);

            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpBuscarProv.Size.Width);

            locationTlpBuscarProv = this.InitialRealPointVertical + dgvMostrarProveedores.Size.Height + 15;

            tlpBuscarProv.Location = new Point(wid, locationTlpBuscarProv);

            
        }
                
        private void formatearTablaBuscarRazon()
        {
           
            locationTlpBuscarProvRazon = locationTlpBuscarProv + tlpBuscarProv.Size.Height;

            tlpBuscarProvRazon.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarProvRazon.Size.Width), locationTlpBuscarProvRazon);

            tlpBuscarProvRazon.Controls.Add(btBuscarProvRaz);
            tlpBuscarProvRazon.Controls.Add(tbBuscarProvRazonS);


        }

        private void formatearTablaBotones()
        {            
            locationBtCambios = locationTlpBuscarProvRazon + tlpBuscarProvRazon.Size.Height + 10;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), locationBtCambios);
            
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btGuardarCambios);

        }

     

        #endregion

        #region INICIALIZAR COMPONENTE
        private void InitializeComponent()
        {
            this.dgvMostrarProveedores = new System.Windows.Forms.DataGridView();
            this.tlpBuscarProv = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProvCuil = new System.Windows.Forms.TextBox();
            this.tlpBuscarProvRazon = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProvRazonS = new System.Windows.Forms.TextBox();
            this.btBuscarProvCuil = new System.Windows.Forms.Button();
            this.btBuscarProvRaz = new System.Windows.Forms.Button();
            this.btGuardarCambios = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrarProveedores)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMostrarProveedores
            // 
            this.dgvMostrarProveedores.AllowUserToResizeRows = false;
            this.dgvMostrarProveedores.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrarProveedores.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrarProveedores.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrarProveedores.Name = "dgvMostrarProveedores";
            this.dgvMostrarProveedores.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrarProveedores.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrarProveedores.TabIndex = 0;
            this.dgvMostrarProveedores.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarProveedores_CellContentClick);
            // 
            // tlpBuscarProv
            // 
            this.tlpBuscarProv.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProv.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProv.ColumnCount = 2;
            this.tlpBuscarProv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProv.Name = "tlpBuscarProv";
            this.tlpBuscarProv.RowCount = 1;
            this.tlpBuscarProv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarProv.TabIndex = 0;
            // 
            // tbBuscarProvCuil
            // 
            this.tbBuscarProvCuil.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvCuil.MaxLength = 11;
            this.tbBuscarProvCuil.Name = "tbBuscarProvCuil";
            this.tbBuscarProvCuil.Size = new System.Drawing.Size(140, 30);
            this.tbBuscarProvCuil.TabIndex = 0;
            // 
            // tlpBuscarProvRazon
            // 
            this.tlpBuscarProvRazon.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarProvRazon.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProvRazon.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProvRazon.ColumnCount = 2;
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProvRazon.Name = "tlpBuscarProvRazon";
            this.tlpBuscarProvRazon.RowCount = 1;
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarProvRazon.TabIndex = 0;
            // 
            // tbBuscarProvRazonS
            // 
            this.tbBuscarProvRazonS.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.tbBuscarProvRazonS.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvRazonS.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvRazonS.MaxLength = 40;
            this.tbBuscarProvRazonS.Name = "tbBuscarProvRazonS";
            this.tbBuscarProvRazonS.Size = new System.Drawing.Size(220, 30);
            this.tbBuscarProvRazonS.TabIndex = 0;
            // 
            // btBuscarProvCuil
            // 
            this.btBuscarProvCuil.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProvCuil.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvCuil.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvCuil.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvCuil.Name = "btBuscarProvCuil";
            this.btBuscarProvCuil.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvCuil.TabIndex = 0;
            this.btBuscarProvCuil.Text = "Buscar Proveedor (Cuil)";
            this.btBuscarProvCuil.UseVisualStyleBackColor = false;
            this.btBuscarProvCuil.Click += new System.EventHandler(this.btBuscarProvCuil_Click);
            // 
            // btBuscarProvRaz
            // 
            this.btBuscarProvRaz.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProvRaz.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvRaz.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvRaz.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvRaz.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvRaz.Name = "btBuscarProvRaz";
            this.btBuscarProvRaz.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvRaz.TabIndex = 0;
            this.btBuscarProvRaz.Text = "Buscar Proveedor (Razón Social)";
            this.btBuscarProvRaz.UseVisualStyleBackColor = false;
            this.btBuscarProvRaz.Click += new System.EventHandler(this.btBuscarProvRaz_Click);
            // 
            // btGuardarCambios
            // 
            this.btGuardarCambios.BackColor = System.Drawing.Color.AliceBlue;
            this.btGuardarCambios.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btGuardarCambios.Location = new System.Drawing.Point(0, 0);
            this.btGuardarCambios.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btGuardarCambios.Name = "btGuardarCambios";
            this.btGuardarCambios.Size = new System.Drawing.Size(75, 40);
            this.btGuardarCambios.TabIndex = 0;
            this.btGuardarCambios.Text = "Guardar";
            this.btGuardarCambios.UseVisualStyleBackColor = false;
            this.btGuardarCambios.Click += new System.EventHandler(this.btGuardarCambios_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // ProveedorPanelModificar
            // 
            this.AutoSize = true;
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrarProveedores)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void btBuscarProvCuil_Click(object sender, EventArgs e)
        {
            dgvMostrarProveedores.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarProvCuil.Text)&&(!(tbBuscarProvCuil.Text.Length<11)))
            {
                string searchValue = tbBuscarProvCuil.Text;
                int inRowFinded = -1;
                
                try
                {
                    foreach (DataGridViewRow row in dgvMostrarProveedores.Rows)
                    {
                        if (row.Cells[3].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrarProveedores.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;
                            inRowFinded = row.Index;
                         //   MessageBox.Show("ENCONTRE: " + inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMostrarProveedores.CurrentCell = row.Cells[0];
                            tbBuscarProvCuil.Clear();

                            break;
                        }
                    }                    
                   
                }
                catch (Exception exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    //CLogger.Append("[btBuscarProvCuil_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                    tbBuscarProvCuil.Clear();
                    MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else 
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NUMERO DE CUIL \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    
            
            }
        }

        private void btBuscarProvRaz_Click(object sender, EventArgs e)
        {
            dgvMostrarProveedores.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarProvRazonS.Text))
            {
                string searchValue = tbBuscarProvRazonS.Text;
                int inRowFinded = -1;                
                try
                {
                    foreach (DataGridViewRow row in dgvMostrarProveedores.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrarProveedores.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                            row.Selected = true;
                           
                            inRowFinded = row.Index;
                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvMostrarProveedores.CurrentCell = row.Cells[0];
                            tbBuscarProvRazonS.Clear();
                            break;
                            
                        }
                    }            
                }
                catch (Exception exc)
                {
                    tbBuscarProvRazonS.Clear();
                    //MessageBox.Show(exc.Message); //DEBUG
                    //CLogger.Append("[btBuscarProvRaz_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                    MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO RAZÓN SOCIAL", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void dgvMostrarProveedores_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("PRESIONE", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            dgvMostrarProveedores.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btGuardarCambios_Click(object sender, EventArgs e)
        {
            try
            {
                if (adapter != null)
                {
                    cmdBuilder = new SqlCommandBuilder(adapter);
                    changes = ds.GetChanges();
                    if (changes != null)
                    {                        
                        adapter.Update(changes);
                        MessageBox.Show("CAMBIOS GUARDADOS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else 
                    {
                        MessageBox.Show("NO HAY CAMBIOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }

                }
            }
            catch(Exception exc)
            {
                MessageBox.Show("ERROR AL GUARDAR LOS DATOS.\nNo se guardaran los datos. \nVerifique celtas modificadas y cantidad de dígitos modificados.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                CLogger.Append("[btGuardarCambios_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                this.handler.backToPrincipal();
            }
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }


    }
}
